<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Grima Portal</title>
	<link rel="stylesheet" type="text/css" href="../css/themes/bootstrap/easyui.css">
	<link rel="stylesheet" type="text/css" href="../css/icon.css">
	<link rel="stylesheet" type="text/css" href="../css/portal.css">
	<link rel="stylesheet" type="text/css" href="../css/font-awesome.min.css">
	
	<script type="text/javascript" src="../js/bootstrap.js"></script>
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
</head>

<body class="easyui-layout">

    <div data-options="region:'north',border:false" style="height:50px;padding:10px;overflow: hidden;">
		<jsp:include page="header.jsp" />
	</div>
    <div data-options="region:'east',split:true,collapsed:true,title:' '" style="width:250px;padding:10px;"></div>
	<div data-options="region:'south',border:false" style="height:30px;padding:10px;background:#F5F5F5;text-align:center;color:#707070">
		<jsp:include page="footer.jsp" />
	</div>

    <div data-options="region:'center',title:'Descritivo ${descriptive.id} : ${descriptive.collection}'">
		<div style="margin:5px">
			<a href="#" title="Gravar" onclick="saveDescriptive();"><i class="fa fa-save" style="font-size:18px;color:green;padding:5px;"></i>Salvar</a>
			<a href="descriptives.html" title="Voltar"><i class="fa fa-mail-reply" style="font-size:18px;color:green;padding:5px;"></i>Voltar</a>
		</div>
		<br/>

		<c:url var="url" value="/secure/saveDescriptive.html" />
	    <form:form id="descriptiveForm" action="${url}" method="POST" modelAttribute="descriptive">
			<div class="easyui-tabs" >

				<div title="Itens" style="padding:10px">
					<div id="descriptiveItems">
						<jsp:include page="descriptiveItemsEdit.jsp" />
					</div>		
				</div>
				
				<div title="Descritivo" style="padding:10px">
					<div id="descriptiveHeader">
						<jsp:include page="descriptiveHeaderEdit.jsp" />
					</div>
				</div>
	
			</div>
		</form:form>

	</div>
	
	<script>
		function saveDescriptive(){
			endEditing();

			var rows = $('#dg').datagrid('getRows');
			$.each(rows,function(i,row) {
			    if (isValid(row)){
		            $('#hiddenFields').append('<input type="hidden" id="id_'+i+'" name="items['+i+'].id" value="'+row.id+'" />');
		            $('#hiddenFields').append('<input type="hidden" id="action_'+i+'" name="items['+i+'].action" value="'+row.action+'" />');
		            $('#hiddenFields').append('<input type="hidden" id="reference_'+i+'" name="items['+i+'].reference" value="'+row.reference+'" />');
	           		$('#hiddenFields').append('<input type="hidden" id="description_'+i+'" name="items['+i+'].description" value="'+row.description+'" />');
	           		$('#hiddenFields').append('<input type="hidden" id="status_'+i+'" name="items['+i+'].status" value="'+row.status+'" />');
	           		$('#hiddenFields').append('<input type="hidden" id="sizes_'+i+'" name="items['+i+'].sizes" value="'+row.sizes+'" />');
	 				$('#hiddenFields').append('<input type="hidden" id="colors_'+i+'" name="items['+i+'].colors" value="'+row.colors+'" />');
	 				$('#hiddenFields').append('<input type="hidden" id="setas_'+i+'" name="items['+i+'].setas" value="'+row.setas+'" />');
	 				$('#hiddenFields').append('<input type="hidden" id="fotografarTambem_'+i+'" name="items['+i+'].fotografarTambem" value="'+row.fotografarTambem+'" />');
	 				$('#hiddenFields').append('<input type="hidden" id="lookbookCor_'+i+'" name="items['+i+'].lookbookCor" value="'+row.lookbookCor+'" />');
	 				$('#hiddenFields').append('<input type="hidden" id="correcoes_'+i+'" name="items['+i+'].correcoes" value="'+row.correcoes+'" />');
	 				$('#hiddenFields').append('<input type="hidden" id="comments_'+i+'" name="items['+i+'].comments" value="'+row.comments+'" />');
					$('#hiddenFields').append('<input type="hidden" id="descriptive_id_'+i+'" name="items['+i+'].descriptive.id" value="${descriptive.id}" />');
			    }
			});

			$('form#descriptiveForm').submit();
		}

		function isValid(row){
			if ( 
				 ( row.action == "save") &&
			     ( row.reference != "" || 
				   row.description != "" || 
				   row.status != "" ||
				   row.sizes != "" || 
				   row.colors != "" ||
				   row.setas != "" ||
				   row.fotografarTambem != "" ||
				   row.lookbookCor != "" ||
				   row.correcoes != "" ||
				   row.comments != "" )
			   ){
				
				return true;
			}
			return false;
		}
	</script>
	
</body>
</html>